package com.utooo.service.supplier;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.dao.purchase.*;
import com.utooo.dao.purchase.report.OrderExecutionReportMapper;
import com.utooo.dao.supplier.*;
import com.utooo.pojo.domain.purchase.DemandMaterialDetailDO;
import com.utooo.pojo.domain.purchase.order.*;
import com.utooo.pojo.domain.purchase.price.HistoricalTransactionPriceDO;
import com.utooo.pojo.domain.purchase.report.OrderExecutionReportDO;
import com.utooo.pojo.domain.purchase.report.OrderSettlementReportDO;
import com.utooo.pojo.domain.supplier.execute.*;
import com.utooo.pojo.dto.supplier.deliveryplan.DeliveryPlanUpdateDto;
import com.utooo.pojo.dto.supplier.execution.PurchasePushDto;
import com.utooo.pojo.dto.supplier.execution.PurchaseUpdateDto;
import com.utooo.util.GenerateReceiptNo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.utooo.constant.Constans.IGNORED_PROPERTIES;

/**
 * @Description: 订单执行相关接口
 * @Author SJD
 * @Date: 2025/4/22 14:12
 * @Copyright: 南京优通信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class PurchaseExecutionServiceImpl extends ServiceImpl<PurchaseExecutionMapper, SupplierPurchaseExecutionDO>
        implements PurchaseExecutionService {
    private final PurchaseOrderMapper buyerPurchaseMapper;
    private final DemandMaterialDetailMapper materialDetailMapper;
    private final SupplierDeliveryPlanMapper supplierDeliveryPlanMapper;
    private final BuyerDeliveryPlanMapper buyerDeliveryPlanMapper;
    private final DeliveryPlanMaterialMapper deliveryPlanMaterialMapper;
    private final SupplierOrderDeliveryRecordMapper orderDeliveryRecordMapper;
    private final SupplierDeliveryMaterialMapper supplierDeliveryMaterialMapper;
    private final HistoricalTransactionPriceMapper historicalTransactionPriceMapper;
    private final OrderExecutionReportMapper orderExecutionReportMapper;
    private final PurchaseExecutionMapper purchaseExecutionMapper;
    private final OrderReceiptMapper orderReceiptMapper;
    private final OrderReturnMapper orderReturnMapper;
    private final OrderReconciliationMapper orderReconciliationMapper;
    private final OrderInvoiceAssociationMapper invoiceAssociationMapper;

    @Override
    public Boolean pushPurchase(PurchasePushDto purchaseExecutionDto) {

        // 获取采货方订单执行实体类
        PurchaseOrderDO purchaseOrderDO = buyerPurchaseMapper.selectOne(new LambdaQueryWrapper<PurchaseOrderDO>().eq(PurchaseOrderDO::getOrderNo, purchaseExecutionDto.getOrderNumber()));
        if (null == purchaseOrderDO) {
            return Boolean.FALSE;
        }

        SupplierPurchaseExecutionDO supplierPurchaseExecutionDO = BeanUtil.copyProperties(purchaseOrderDO, SupplierPurchaseExecutionDO.class, IGNORED_PROPERTIES);
        supplierPurchaseExecutionDO.setStatus("1");
        supplierPurchaseExecutionDO.setCreateTime(LocalDateTime.now());
        supplierPurchaseExecutionDO.setId(null);
        supplierPurchaseExecutionDO.setOrderNo(purchaseOrderDO.getOrderNo());
        // 保存订单执行实体类
        this.save(supplierPurchaseExecutionDO);
        // 获取采货方物料实体类 并且更新foreignId和id
        List<DemandMaterialDetailDO> materialDetailDOList = materialDetailMapper.selectList(new LambdaQueryWrapper<DemandMaterialDetailDO>().eq(DemandMaterialDetailDO::getForeignId, purchaseOrderDO.getId()));
        materialDetailDOList.forEach(materialDetailDO -> {
            materialDetailDO.setForeignId(supplierPurchaseExecutionDO.getId());
            materialDetailDO.setId(null);
        });
        materialDetailMapper.insert(materialDetailDOList);
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean updatePurchase(PurchaseUpdateDto purchaseUpdateDto) {
        // 根据订单编号查询订单
        QueryWrapper<SupplierPurchaseExecutionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("selectField114", purchaseUpdateDto.getOrderNumber());
        SupplierPurchaseExecutionDO supplierPurchaseExecutionDO = this.getOne(queryWrapper);

        // 如果订单不存在，返回false
        if (supplierPurchaseExecutionDO == null) {
            return Boolean.FALSE;
        }

        // 更新订单状态
        supplierPurchaseExecutionDO.setStatus(purchaseUpdateDto.getStatus());
        supplierPurchaseExecutionDO.setUpdateTime(LocalDateTime.now());
        // 保存更新后的订单
        this.updateById(supplierPurchaseExecutionDO);

        //如果订单状态为已确认，则新增一条交货计划
        addDeliveryPlan(supplierPurchaseExecutionDO);
        return true;
    }

    @Override
    public Boolean pushDeliveryPlan(DeliveryPlanUpdateDto param) {
        LambdaQueryWrapper<SupplierDeliveryPlanDO> queryWrapper = new LambdaQueryWrapper<>();
        //根据交货计划编号查询交货计划  这应该是唯一的
        queryWrapper.eq(SupplierDeliveryPlanDO::getPlanNumber, param.getPlanNumber());
        SupplierDeliveryPlanDO planDO = supplierDeliveryPlanMapper.selectOne(queryWrapper);

        // 如果订单不存在，返回false
        if (planDO == null) {
            return Boolean.FALSE;
        }

        // 根据订单编号 查询采方数据
        // 供方状态1-待确认 2-已确认 3-拒绝 4-二次确认
        // 采方状态1-待确认 2-已确认 3-拒绝 4-待推送
        BuyerDeliveryPlanDO buyerDeliveryPlanDO = buyerDeliveryPlanMapper.selectOne(new LambdaQueryWrapper<BuyerDeliveryPlanDO>()
                .eq(BuyerDeliveryPlanDO::getPlanNumber, param.getPlanNumber()));
        //首先直接赋值供方状态
        planDO.setSupplierStatus(param.getStatus());
        // 如果 入参供方状态为已确认 则采购方状态为待推送则调整为已推送,且新建数据 推送到采方门户 采方为待确认
        if ("2".equals(param.getStatus())) {
            planDO.setSupplierStatus("2");
            planDO.setBuyerStatus("1");
            // 为空则直接赋值
            if(null == buyerDeliveryPlanDO) {
                buyerDeliveryPlanDO = new BuyerDeliveryPlanDO();
                BeanUtil.copyProperties(planDO, buyerDeliveryPlanDO, IGNORED_PROPERTIES);
                buyerDeliveryPlanMapper.insert(buyerDeliveryPlanDO);
            }else {
                // 不为空直接复制
                BeanUtil.copyProperties(planDO, buyerDeliveryPlanDO, IGNORED_PROPERTIES);
                buyerDeliveryPlanMapper.updateById(buyerDeliveryPlanDO);
            }
            supplierDeliveryPlanMapper.updateById(planDO);
            // 这里就是发生在供方推送后,发生的二次确认或者拒绝
        }

        //获取供货方发货计划物料实体类 并且更新foreignId和id 然后插回数据库
        //需要先根据id删除 再插入 子表不存在更新
        assert buyerDeliveryPlanDO != null;
        // 供方修改数据时候 应该是修改采方数据
        deliveryPlanMaterialMapper.delete(new LambdaQueryWrapper<DeliveryPlanMaterialDO>().eq(DeliveryPlanMaterialDO::getForeignId, buyerDeliveryPlanDO.getId()));
        List<DeliveryPlanMaterialDO> suspplierMaterialDOList = deliveryPlanMaterialMapper.selectList(new LambdaQueryWrapper<DeliveryPlanMaterialDO>()
                .eq(DeliveryPlanMaterialDO::getForeignId, planDO.getId()));
        BuyerDeliveryPlanDO finalBuyerDeliveryPlanDO = buyerDeliveryPlanDO;
        suspplierMaterialDOList.forEach(materialDO -> {
            materialDO.setId(null);
            materialDO.setForeignId(finalBuyerDeliveryPlanDO.getId());
            materialDO.setCreateTime(LocalDateTime.now());
            materialDO.setOrderNo(finalBuyerDeliveryPlanDO.getOrderNo());
        });
        deliveryPlanMaterialMapper.insert(suspplierMaterialDOList);

        if (!"2".equals(param.getStatus())) {
            buyerDeliveryPlanDO.setUpdateTime(LocalDateTime.now());
            // 如果 入参供方状态为拒绝 则采购方状态为待推送
            if ("3".equals(param.getStatus())) {
                planDO.setSupplierStatus("3");
                //  采购方状态为拒绝 采方状态不变 这里流程就走不下去了 死掉了 拒绝后应该还是有状态的 todo
                // 如果采购方状态为二次确认,则进行发货 无需更改采方状态
            } else if ("4".equals(param.getStatus())) {
                planDO.setSupplierStatus("4");
                // 生成发货计划
                insertDeliveryPlan(planDO, suspplierMaterialDOList);
            }
            // 更新数据表
            buyerDeliveryPlanDO.setSupplierStatus(planDO.getSupplierStatus());
            buyerDeliveryPlanDO.setBuyerStatus(planDO.getBuyerStatus());
            supplierDeliveryPlanMapper.updateById(planDO);
            buyerDeliveryPlanMapper.updateById(buyerDeliveryPlanDO);
        }
        return true;
    }

    @Override
    public Boolean auditDeliveryPlan(DeliveryPlanUpdateDto param) {

        // 获取采方实体类
        BuyerDeliveryPlanDO buyerDeliveryPlanDO = buyerDeliveryPlanMapper.selectOne(new LambdaQueryWrapper<BuyerDeliveryPlanDO>()
                .eq(BuyerDeliveryPlanDO::getPlanNumber, param.getPlanNumber()));
        // 获取供方实体类
        SupplierDeliveryPlanDO supplierDeliveryPlanDO = supplierDeliveryPlanMapper.selectOne(new LambdaQueryWrapper<SupplierDeliveryPlanDO>()
                .eq(SupplierDeliveryPlanDO::getPlanNumber, param.getPlanNumber()));
        // 获取 采方修改后的物料
        List<DeliveryPlanMaterialDO> suspplierMaterialDOList = deliveryPlanMaterialMapper.selectList(new LambdaQueryWrapper<DeliveryPlanMaterialDO>()
                .eq(DeliveryPlanMaterialDO::getForeignId, buyerDeliveryPlanDO.getId()));
        buyerDeliveryPlanDO.setBuyerStatus(param.getStatus());
        //采方状态直接赋值
        supplierDeliveryPlanDO.setBuyerStatus(param.getStatus());
        BeanUtil.copyProperties(buyerDeliveryPlanDO, supplierDeliveryPlanDO, IGNORED_PROPERTIES);
        // 采采方的状态 去判断逻辑 然后设置采方的值
        // 采方同意
        if ("2".equals(param.getStatus())) {
            // 同意 而且供方状态为 已确认(确认就是未修改数据)则是修改供方状态为已二次确认
            if ("2".equals(buyerDeliveryPlanDO.getSupplierStatus())) {
                buyerDeliveryPlanDO.setSupplierStatus("4");
                // 生成发货计划
                insertDeliveryPlan(supplierDeliveryPlanDO, suspplierMaterialDOList);
                // 同意但是供方为待确认则无需修改 供方状态(此时供方为待确认)
            }
            // 拒绝则是待确认
        } else if ("3".equals(param.getStatus())) {
            // 更新供方状态为待确认
            buyerDeliveryPlanDO.setSupplierStatus("1");
        }

        // 采方审核 采方两个状态都是好的 要将采方两个状态同步到供方
        supplierDeliveryPlanDO.setSupplierStatus(buyerDeliveryPlanDO.getSupplierStatus());
        supplierDeliveryPlanDO.setBuyerStatus(buyerDeliveryPlanDO.getBuyerStatus());
        // 更新数据表
        supplierDeliveryPlanMapper.updateById(supplierDeliveryPlanDO);
        buyerDeliveryPlanMapper.updateById(buyerDeliveryPlanDO);

        // 采方修改数据的时候 应该是修改供方物料表
        deliveryPlanMaterialMapper.delete(new LambdaQueryWrapper<DeliveryPlanMaterialDO>().eq(DeliveryPlanMaterialDO::getForeignId, supplierDeliveryPlanDO.getId()));

        suspplierMaterialDOList.forEach(materialDO -> {
            materialDO.setId(null);
            materialDO.setForeignId(supplierDeliveryPlanDO.getId());
            materialDO.setCreateTime(LocalDateTime.now());
            materialDO.setOrderNo(buyerDeliveryPlanDO.getOrderNo());

        });
        deliveryPlanMaterialMapper.insert(suspplierMaterialDOList);
        return true;
    }

    @Override
    public Boolean pushTransactionPrice(PurchasePushDto purchaseExecutionDto) {
        // 获取订单数据
        PurchaseOrderDO orderDO = buyerPurchaseMapper.selectOne(new LambdaQueryWrapper<PurchaseOrderDO>()
                .eq(PurchaseOrderDO::getOrderNo, purchaseExecutionDto.getOrderNumber()));
        // 获取采货方物料实体类 并且更新foreignId和id
        List<DemandMaterialDetailDO> materialDetailDOList = materialDetailMapper.selectList(new LambdaQueryWrapper<DemandMaterialDetailDO>().eq(DemandMaterialDetailDO::getForeignId, orderDO.getId()));

        List<HistoricalTransactionPriceDO> transactionPriceDOList = BeanUtil.copyToList(materialDetailDOList, HistoricalTransactionPriceDO.class);
        transactionPriceDOList.forEach(priceDo->{
            priceDo.setId(null);
            priceDo.setForeignId(null);
            priceDo.setPurchaseOrderNo(orderDO.getOrderNo());
            priceDo.setPriceGenerationTime(LocalDateTime.now());
        });

        historicalTransactionPriceMapper.insert(transactionPriceDOList);
        return true;
    }

    @Override
    public Boolean executeOrderExecution() {
        // 获取所有已完成的id
        // 审批中->待确认->已确认->执行中->已完成
        List<OrderExecutionReportDO> executionReportDOS = orderExecutionReportMapper.selectList(new LambdaQueryWrapper<OrderExecutionReportDO>()
                .eq(OrderExecutionReportDO::getExecutionStatus, "5"));
        List<String> completedOrderIds = executionReportDOS.stream().map(OrderExecutionReportDO::getOrderNo).toList();
        // 这是所有订单

        List<PurchaseOrderDO> purchaseOrderDOS = buyerPurchaseMapper.selectList(
                new LambdaQueryWrapper<PurchaseOrderDO>()
                        .notIn(!completedOrderIds.isEmpty(), PurchaseOrderDO::getOrderNo, completedOrderIds).isNotNull(PurchaseOrderDO::getOrderNo)
        );
        List<String> orderIds = purchaseOrderDOS.stream().map(PurchaseOrderDO::getOrderNo) .toList();
        List<OrderExecutionReportDO> resultList = BeanUtil.copyToList(purchaseOrderDOS, OrderExecutionReportDO.class);
        // 查询确认表--已经确认的
        List<SupplierPurchaseExecutionDO> supplierPurchaseExecutionDOS = purchaseExecutionMapper.selectList(new LambdaQueryWrapper<SupplierPurchaseExecutionDO>().eq(SupplierPurchaseExecutionDO::getStatus, "2").in(SupplierPurchaseExecutionDO::getOrderNo, orderIds));
        List<String> confirmedList = supplierPurchaseExecutionDOS.stream().map(SupplierPurchaseExecutionDO::getOrderNo).toList();
        // 查询发货表
        List<SupplierOrderDeliveryRecordDO> deliveryRecordList = orderDeliveryRecordMapper.selectList(new LambdaQueryWrapper<>());
        List<String> deliveryIdList = deliveryRecordList.stream().map(SupplierOrderDeliveryRecordDO::getId).toList();

        List<SupplierDeliveryMaterialDO> deliveryMaterialDOList = supplierDeliveryMaterialMapper.selectList(new LambdaQueryWrapper<SupplierDeliveryMaterialDO>()
                .eq(SupplierDeliveryMaterialDO::getForeignId, deliveryIdList));
        // 获取到发货数量
        Map<String, Integer> deliverySummaryMap = deliveryMaterialDOList.stream()
                .collect(Collectors.groupingBy(
                        SupplierDeliveryMaterialDO::getOrderNo, Collectors.summingInt(SupplierDeliveryMaterialDO::getMaxShipmentQuantity)
                ));
        // 查询收货数量
        List<OrderReceiptDO> orderReceiptDOS = orderReceiptMapper.selectList(new LambdaQueryWrapper<>());
        Map<String, Integer> orderReceiptMap = orderReceiptDOS.stream()
                .collect(Collectors.groupingBy(
                        OrderReceiptDO::getOrderNo, Collectors.summingInt(OrderReceiptDO::getReceivedTotalQuantity)
                ));

        // 查询退货数量
        List<OrderReturnDO> orderReturnDOS = orderReturnMapper.selectList(new LambdaQueryWrapper<>());
        Map<String, Integer> orderReturnList = orderReturnDOS.stream()
                .collect(Collectors.groupingBy(
                        OrderReturnDO::getOrderNo, Collectors.summingInt(OrderReturnDO::getTotalReturnQuantity)
                ));
        resultList.forEach(result -> {
            // 待确认->已确认->执行中->已完成
            //初始状态待确认
            result.setId(null);
            result.setExecutionStatus("1");
            if(confirmedList.contains(result.getOrderNo())){
                result.setExecutionStatus("2");
            }
            // 设置最大发货数量
            if(deliverySummaryMap.containsKey(result.getOrderNo())){
                result.setExecutionStatus("3");
                result.setMaxDeliveryQuantity(deliverySummaryMap.get(result.getOrderNo()));
            }
            // 设置收货数量
            if(orderReceiptMap.containsKey(result.getOrderNo())){
                result.setExecutionStatus("3");
                result.setReceivedQuantity(orderReceiptMap.get(result.getOrderNo()));
            }
            // 设置退货数量
            if(orderReturnList.containsKey(result.getOrderNo())){
                result.setExecutionStatus("3");
                result.setReturnedQuantity(orderReturnList.get(result.getOrderNo()));
            }
            // 在途=发货-收货-退货
            result.setInTransitQuantity(result.getMaxDeliveryQuantity() - result.getReceivedQuantity() - result.getReturnedQuantity());
            // 需求数量=收货+退货 则是已完成
            int quantity = result.getQuantity() != null ? result.getQuantity() : 0;
            if(quantity == result.getReceivedQuantity() + result.getReturnedQuantity()){
                result.setExecutionStatus("4");
            }
        });
        // 批量插入或更新
        orderExecutionReportMapper.upsertBatch(resultList);
        return Boolean.TRUE;
    }

    @Override
    public Boolean executeOrderSettlement() {
        // 获取所有订单信息
        List<PurchaseOrderDO> purchaseOrderDOS = buyerPurchaseMapper.selectList(
                new LambdaQueryWrapper<PurchaseOrderDO>().isNotNull(PurchaseOrderDO::getOrderNo)
        );
        List<OrderSettlementReportDO> resultList = BeanUtil.copyToList(purchaseOrderDOS, OrderSettlementReportDO.class);
        //抽取订单业务对账表
        List<OrderReconciliationDO> reconciliationDOS = orderReconciliationMapper.selectList(new LambdaQueryWrapper<>());
        // 按照订单编号求和应对含税金额
        Map<String, BigDecimal> reconciliationMap = reconciliationDOS.stream()
                .collect(Collectors.groupingBy(
                        OrderReconciliationDO::getOrderNo,
                        Collectors.reducing(BigDecimal.ZERO, OrderReconciliationDO::getExpectedAmountIncludingTax, BigDecimal::add)
                ));
        // 已确认对账金额
        Map<String, BigDecimal> completeReconciliationMap = reconciliationDOS.stream()
                .filter(reconciliationDO -> reconciliationDO.getStatus().equals("3"))
                .collect(Collectors.groupingBy(
                        OrderReconciliationDO::getOrderNo,
                        Collectors.reducing(BigDecimal.ZERO, OrderReconciliationDO::getActualAmountIncludingTax, BigDecimal::add)
                ));
        // 对账中金额
        Map<String, BigDecimal> completeingReconciliationMap = reconciliationDOS.stream()
                .filter(reconciliationDO -> !reconciliationDO.getStatus().equals("3"))
                .collect(Collectors.groupingBy(
                        OrderReconciliationDO::getOrderNo,
                        Collectors.reducing(BigDecimal.ZERO, OrderReconciliationDO::getActualAmountIncludingTax, BigDecimal::add)
                ));
        // 扣款金额
        // 应关联金额
        List<OrderInvoiceAssociationDO> orderInvoiceAssociationDOS = invoiceAssociationMapper.selectList(new LambdaQueryWrapper<>());
        // 未关联金额
        // 关联发票金额
        // 已关联发票金额
        // 未关联发票金额
        // 预付款金额
        // 预付款退款
        // 待付款金额
        return null;
    }

    private void insertDeliveryPlan(SupplierDeliveryPlanDO supplierDeliveryPlanDO, List<DeliveryPlanMaterialDO> suspplierMaterialDOList) {
        SupplierOrderDeliveryRecordDO supplierOrderDeliveryRecordDO = new SupplierOrderDeliveryRecordDO();
        BeanUtil.copyProperties(supplierDeliveryPlanDO, supplierOrderDeliveryRecordDO, IGNORED_PROPERTIES);

        //生成发货编号
        supplierOrderDeliveryRecordDO.setShipmentNumber(GenerateReceiptNo.getGenerateReceiptNo("ASN", "comInputField125", orderDeliveryRecordMapper));
        supplierOrderDeliveryRecordDO.setId(null);
        supplierOrderDeliveryRecordDO.setCreateTime(LocalDateTime.now());
        supplierOrderDeliveryRecordDO.setStatus("1");
        supplierOrderDeliveryRecordDO.setTotalShipmentQuantity(supplierDeliveryPlanDO.getTotalQuantity());
        orderDeliveryRecordMapper.insert(supplierOrderDeliveryRecordDO);
        List<SupplierDeliveryMaterialDO> deliveryMaterialDOList = BeanUtil.copyToList(suspplierMaterialDOList, SupplierDeliveryMaterialDO.class);
        deliveryMaterialDOList.forEach(materialDO -> {
            materialDO.setId(null);
            materialDO.setCreateTime(LocalDateTime.now());
            materialDO.setForeignId(supplierOrderDeliveryRecordDO.getId());
            materialDO.setOrderNo(supplierDeliveryPlanDO.getOrderNo());
        });
        supplierDeliveryMaterialMapper.insert(deliveryMaterialDOList);
    }

    private void addDeliveryPlan(SupplierPurchaseExecutionDO supplierPurchaseExecutionDO) {
        SupplierDeliveryPlanDO supplierDeliveryPlanDO = new SupplierDeliveryPlanDO();
        BeanUtil.copyProperties(supplierPurchaseExecutionDO, supplierDeliveryPlanDO, IGNORED_PROPERTIES);
        supplierDeliveryPlanDO.setPlanNumber(GenerateReceiptNo.getGenerateReceiptNo("SP", "comInputField176", supplierDeliveryPlanMapper));
        supplierDeliveryPlanDO.setCreateTime(LocalDateTime.now());
        // 设置交货计划状态为待确认
        supplierDeliveryPlanDO.setSupplierStatus("1");
        // 设置采方状态为待推送
        supplierDeliveryPlanDO.setBuyerStatus("4");
        // 设置交货计划创建时间
        supplierDeliveryPlanDO.setCreateTime(LocalDateTime.now());
        supplierDeliveryPlanMapper.insert(supplierDeliveryPlanDO);
        // 还要生成交货的物料表
        List<DemandMaterialDetailDO> materialDetailDOList = materialDetailMapper.selectList(new LambdaQueryWrapper<DemandMaterialDetailDO>()
                .eq(DemandMaterialDetailDO::getForeignId, supplierPurchaseExecutionDO.getId()));
        List<DeliveryPlanMaterialDO> planMaterialDos = BeanUtil.copyToList(materialDetailDOList, DeliveryPlanMaterialDO.class);
        planMaterialDos.forEach(i -> {
            i.setForeignId(supplierDeliveryPlanDO.getId());
            i.setOrderNo(supplierDeliveryPlanDO.getOrderNo());
            i.setCreateTime(LocalDateTime.now());
            i.setId(null);
        });
        deliveryPlanMaterialMapper.insert(planMaterialDos);

    }

}
